﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MySql.Data.MySqlClient;
using System.Configuration;
using System.Text;
using System.Data;

namespace ReconUI
{
    public partial class RemitterMaster : System.Web.UI.Page
    {
        MySqlConnection connection = new MySqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
        MySqlCommand cmd;

        MySqlDataAdapter adapter;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (((UserVO)Session["CurrentUserVO"]) == null)
            {
                Response.Redirect("ReconSignIn.aspx");
            }
            if (!IsPostBack)
            {
                bntbrowse.Enabled = false;
                bntgetdetails.Enabled = false;
                rbOption.Focus();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string resp = string.Empty;
            DBFactory db = new DBFactory();
            DataTable dtCustomerDetails = new DataTable();
            int is_Enabled = 0;

            if (chkIsenabled.Checked)
                is_Enabled = 1;

            if (rbOption.SelectedValue == "1")
            {

                dtCustomerDetails = db.GetRemitterDetailsByRemitterId(Convert.ToString(txtRemitterId.Text.Trim()));

                if (dtCustomerDetails.Rows.Count == 1)
                {
                    //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Remitter already exist!')</script>");
                    lblMessage.Text = "Remitter already exist!";
                    btnClickPopup.Show();
                }
                else
                {
                    int retval = db.AddRemitter(Convert.ToString(txtRemitterId.Text.Trim()), Convert.ToString(txtRemitterName.Text.Trim()), ((UserVO)Session["CurrentUserVO"]).customerId, Convert.ToString(txtAddress1.Text.Trim()), Convert.ToString(txtAddress2.Text.Trim()), Convert.ToString(txtAddress3.Text.Trim()), Convert.ToString(txtCity.Text.Trim()), Convert.ToString(txtState.Text.Trim()), Convert.ToString(txtCountry.Text.Trim()), Convert.ToString(txtZipCode.Text.Trim()), Convert.ToString(txtPhone.Text.Trim()), Convert.ToString(txtEmail.Text.Trim()), is_Enabled, ((UserVO)Session["CurrentUserVO"]).userId , txtDiscountcode.Value ,txtPenaltycode.Value);
                    if (retval != 0)
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Remitter added successfully!')</script>");
                        lblMessage.Text = "Remitter added successfully!";
                        btnClickPopup.Show();
                        ResetFields();
                    }
                    else
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "ErrorMessage", "<script>alert('Error adding Remitter!')</script>");
                        lblMessage.Text = "Error adding Remitter!";
                        btnClickPopup.Show();
                    }
                }
            }
            else
            {

                dtCustomerDetails = db.GetRemitterDetailsByRemitterId(Convert.ToString(txtRemitterId.Text.Trim()));

                if (dtCustomerDetails.Rows.Count <= 0)
                {
                    //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Remitter does not exist!')</script>");
                    lblMessage.Text = "Remitter does not exist!";
                    btnClickPopup.Show();
                }
                else if (dtCustomerDetails.Rows.Count == 1)
                {
                    int retval = db.ModifyRemitter(Convert.ToString(txtRemitterId.Text.Trim()), Convert.ToString(txtRemitterName.Text.Trim()), ((UserVO)Session["CurrentUserVO"]).customerId, Convert.ToString(txtAddress1.Text.Trim()), Convert.ToString(txtAddress2.Text.Trim()), Convert.ToString(txtAddress3.Text.Trim()), Convert.ToString(txtCity.Text.Trim()), Convert.ToString(txtState.Text.Trim()), Convert.ToString(txtCountry.Text.Trim()), Convert.ToString(txtZipCode.Text.Trim()), Convert.ToString(txtPhone.Text.Trim()), Convert.ToString(txtEmail.Text.Trim()), is_Enabled, ((UserVO)Session["CurrentUserVO"]).userId, txtDiscountcode.Value, txtPenaltycode.Value);

                    if (retval != 0)
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('Remitter modified sucessfully!')</script>");
                        lblMessage.Text = "Remitter modified sucessfully!";
                        btnClickPopup.Show();
                        ResetFields();
                    }
                    else
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "ErrorMessage", "<script>alert('Error Modifying Remitter!')</script>");
                        lblMessage.Text = "Error Modifying Remitter!";
                        btnClickPopup.Show();
                    }

                }

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetFields();
        }

        private void ResetFields()
        {
            rbOption.SelectedValue = "1";
            bntgetdetails.Enabled = false;
            bntbrowse.Enabled = false;
            txtRemitterId.Text = "";
            txtRemitterName.Text = "";
            txtAddress1.Text = "";
            txtAddress2.Text = "";
            txtAddress3.Text = "";
            txtCity.Text = "";
            txtState.Text = "";
            txtCountry.Text = "";
            txtZipCode.Text = "";
            txtPhone.Text = "";
            txtEmail.Text = "";
            chkIsenabled.Checked = true;
            txtPenaltycode.Value = "";
            txtDiscountcode.Value = "";
        }

        protected void bntbrowse_Click(object sender, EventArgs e)
        {
            string path;
            path = "RemitterMaster.aspx";
            // txtUserId.Text - Get the value ctl00$BodyHolder$txtUserId from Unique Id of the control txtUserId
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<SCRIPT>window.open('SearchRemitter.aspx?textbox=" + txtRemitterId.Text + "&PageRequest= " + path + "','cal','height=400, width=650, left=300, top=150, location=no, menubar=no, resizable=no,scrollbars=no, titlebar=no, toolbar=no',false);</SCRIPT>");
        }

        protected void bntgetdetails_Click(object sender, EventArgs e)
        {
            DataTable dtCustomerDetails = new DataTable();
            DBFactory objDBfactory = new DBFactory();

            try
            {
                //select remitter_id,remitter_name,customer_id, address1, address2,address3,city,state,country,zipcode,tel_no,email_id,is_enable
                if (txtRemitterId.Text.Trim() != "")
                {
                    dtCustomerDetails = objDBfactory.GetRemitterDetailsByRemitterId(Convert.ToString(txtRemitterId.Text.Trim()));

                    if ((dtCustomerDetails) == null)
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('No record found for this remitter!')</script>");
                        lblMessage.Text = "No record found for this remitter!";
                        btnClickPopup.Show();
                    }
                    else if (dtCustomerDetails.Rows.Count == 1)
                    {
                        ResetFields();

                        txtRemitterId.Text = dtCustomerDetails.Rows[0].ItemArray[0].ToString();
                        txtRemitterName.Text = dtCustomerDetails.Rows[0].ItemArray[1].ToString();
                        //customer_id
                        txtAddress1.Text = dtCustomerDetails.Rows[0].ItemArray[3].ToString();
                        txtAddress2.Text = dtCustomerDetails.Rows[0].ItemArray[4].ToString();
                        txtAddress3.Text = dtCustomerDetails.Rows[0].ItemArray[5].ToString();
                        txtCity.Text = dtCustomerDetails.Rows[0].ItemArray[6].ToString();
                        txtState.Text = dtCustomerDetails.Rows[0].ItemArray[7].ToString();
                        txtCountry.Text = dtCustomerDetails.Rows[0].ItemArray[8].ToString();
                        txtZipCode.Text = dtCustomerDetails.Rows[0].ItemArray[9].ToString();
                        txtPhone.Text = dtCustomerDetails.Rows[0].ItemArray[10].ToString();
                        txtEmail.Text = dtCustomerDetails.Rows[0].ItemArray[11].ToString();

                        if (dtCustomerDetails.Rows[0].ItemArray[12].ToString() == "True" || dtCustomerDetails.Rows[0].ItemArray[12].ToString() == "1")
                            chkIsenabled.Checked = true;
                        else
                            chkIsenabled.Checked = false;

                        txtDiscountcode.Value = dtCustomerDetails.Rows[0].ItemArray[13].ToString();
                        txtPenaltycode.Value = dtCustomerDetails.Rows[0].ItemArray[14].ToString();
                        rbOption.SelectedValue = "2";

                    }
                    else
                    {
                        //ClientScript.RegisterClientScriptBlock(this.GetType(), "SuccessMessage", "<script>alert('No record found for this remitter!')</script>");
                        lblMessage.Text = "No record found for this remitter!";
                        btnClickPopup.Show();
                    }
                }
                else
                {
                    //ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Enter Remitter ID!')</script>");
                    lblMessage.Text = "Enter Remitter ID!";
                    btnClickPopup.Show();

                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if ((dtCustomerDetails) != null)
                {
                    dtCustomerDetails.Dispose();
                }
                dtCustomerDetails = null;

            }
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }

        protected void rbOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rbOption.SelectedValue.Trim() == "1")
            {
                bntbrowse.Enabled = false;
                bntgetdetails.Enabled = false;
            }
            else
            {
                bntbrowse.Enabled = true;
                bntgetdetails.Enabled = true;
            }
        }

        protected void bntbrowseDiscount_Click(object sender, EventArgs e)
        {
            string path;
            path = "RemitterMaster.aspx";
            // txtUserId.Text - Get the value ctl00$BodyHolder$txtUserId from Unique Id of the control txtUserId
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<SCRIPT>window.open('SearchDiscount.aspx?textbox=" + txtDiscountcode.Value + "&PageRequest= " + path + "','cal','height=400, width=650, left=300, top=150, location=no, menubar=no, resizable=no,scrollbars=no, titlebar=no, toolbar=no',false);</SCRIPT>");
        }

        protected void bntbrowsePenalty_Click(object sender, EventArgs e)
        {
            string path;
            path = "RemitterMaster.aspx";
            // txtUserId.Text - Get the value ctl00$BodyHolder$txtUserId from Unique Id of the control txtUserId
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<SCRIPT>window.open('SearchPenalty.aspx?textbox=" + txtPenaltycode.Value + "&PageRequest= " + path + "','cal','height=400, width=650, left=300, top=150, location=no, menubar=no, resizable=no,scrollbars=no, titlebar=no, toolbar=no',false);</SCRIPT>");
        }

    }
}